import 'package:kkhousekeep/models/notice_detail_entity.dart';
import 'package:kkhousekeep/models/notice_list_entity.dart';
import 'package:kkhousekeep/net/api.dart';
import 'package:kkhousekeep/net/respnse_code.dart';

import '../models/base_respond_entity.dart';
import '../tools/dialog_util.dart';
import 'base_connect.dart';

class HomeNoticeConnect extends BaseConnect {
  Future<NoticeListEntity> getNoticeList({required bool isOwner}) async {
    var response =
        await post(isOwner ? Api.getNoticeList : Api.getPropertyNoticeList, {});
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value(
        NoticeListEntity().copyWith(
            code: ResponseCode.timeout,
            obj: NoticeListObj().copyWith(list: [], total: 0),
            msg: ""),
      );
    }
    NoticeListEntity entity = NoticeListEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<NoticeDetailEntity> getNoticeDetail(
      {required int id, required bool isOwner}) async {
    var response =
        await post(isOwner ? Api.getNoticeInfo : Api.getPropertyNoticeInfo, {
      'id': id,
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return NoticeDetailEntity().copyWith(
          code: ResponseCode.timeout,
          obj: NoticeDetailObj().copyWith(
            id: -1,
            title: "",
            publisher: "",
            context: "",
            views: -1,
            publishTime: "",
            createTime: "",
            updateTime: "",
            cid: -1,
            read: -1,
          ),
          msg: "");
    }
    NoticeDetailEntity entity = NoticeDetailEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<BaseRespondEntity?> noticeAllRead({required bool isOwner}) async {
    var response =
        await post(isOwner ? Api.noticeAllRead : Api.propertyNoticeAllRead, {});
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    BaseRespondEntity entity = BaseRespondEntity.fromJson(response.body);
    return Future.value(entity);
  }
}
